import mysql.connector
import config

def connet_to_kkomia():
    cnx = None
    cursor = None
    try:
        cnx = mysql.connector.connect(**config.kkomia_family_db)
        cursor = cnx.cursor()
    except Exception as e:
        print("Connected failed:" + str(e))
        if cursor:
            cursor.close()
        if cnx:
            cnx.close()
    
    print("Conneted to tbl_family_member Database successfully")
    try:
        query = ("SELECT * FROM tbl_family_member")

        cursor.execute(query, )
        rows = cursor.fetchall()

    except Exception as e:
        print("Corsor.execute failed:" + str(e))
        if cursor:
            cursor.close()
        if cnx:
            cnx.close()

    data_list = list()
    for row in rows:
        data_list.append({"id": row[0],
                          "name": row[1],
                          "sex": row[3],
                          "phone_num": row[5]})

    for data in data_list:
        print(data)

    cursor.close()
    cnx.close()
    print("Closed the database")